package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Coalprice;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class CoalPriceAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<Coalprice> ctCoaltypeList;
    private Context mContext;
    private int mPosition;

    public CoalPriceAdapter(List<Coalprice> list, Context context) {
        this.ctCoaltypeList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (ctCoaltypeList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.coal_price_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            mPosition = position;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(position);
            viewHolder.SetViewListener(position);
        }
    }

    @Override
    public int getItemCount() {
        if (ctCoaltypeList.size() > 0) {
            return ctCoaltypeList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        return 1;
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView infoTv;
        TextView timeTv;
        TextView userTv;
        TextView sortTv;
        TextView remarkTv;

        public ViewHolder(View itemView) {
            super(itemView);
            infoTv = (TextView) itemView.findViewById(R.id.info);
            timeTv = (TextView) itemView.findViewById(R.id.time);
            userTv = (TextView) itemView.findViewById(R.id.user);
            sortTv = (TextView) itemView.findViewById(R.id.status_label);
            remarkTv = (TextView) itemView.findViewById(R.id.remark);
        }

        public void BindViewData(int position) {
            Coalprice coalprice = ctCoaltypeList.get(position);
            sortTv.setText((position + 1) + "");
            infoTv.setText("煤种：" + coalprice.getCoaltypename() + "  单价：" +
                    coalprice.getUnitprice());
            if (coalprice.getEndtime() != null) {
                timeTv.setText("开始时间：" + TimeUtils.parseDateTime(coalprice.getStarttime())
                        + " " + "到 " + TimeUtils.parseDateTime(coalprice.getEndtime()));
            } else {
                timeTv.setText("开始时间：" + TimeUtils.parseDateTime(coalprice.getStarttime()));
            }
            userTv.setText("操作员：" + coalprice.getAddusername());
            if (coalprice.getRemark() != null) {
                remarkTv.setVisibility(View.VISIBLE);
                remarkTv.setText(coalprice.getRemark());
            } else {
                remarkTv.setVisibility(View.VISIBLE);
            }
        }

        public void SetViewListener(final int position) {

        }

    }
}
